package uoc.ei.practica;

import java.util.Calendar;
import java.util.Date;

/**
 * Planificador sobre las pistes de pádel
 *
 */
public class Schedule {
	private static final int MAX_SLOTS=15;
	private static final int INI=9;
	private static final int DAYS_OF_WEEK=7;

	private int numOfCourts;
	Slot[][] slots;

	public Schedule(int numOfCourts) {
		this.numOfCourts=numOfCourts;
		this.slots=new Slot[DAYS_OF_WEEK][MAX_SLOTS];
	}

	/**
	 * método que realiza una reserva sobre una ranura (slot) de tiempo
	 */
	public Reserve booking(Date date, int duration) throws EIException {
		// TODO Auto-generated method stub
		
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		
		int hour =cal.get(Calendar.HOUR_OF_DAY);
		int day=cal.get(Calendar.DAY_OF_WEEK);
		
		int slotId = hour-INI;
		Slot slot=null;
		
		
		for (int i=slotId; i<slotId+duration; i++) {
			slot= this.slots[day][i];
			if (slot==null) {
				
				slot = new Slot(numOfCourts);
				this.slots[day][i]=slot;
			}
			if (!slot.book()) throw new EIException(Messages.NO_COURTS);
		}
		
		return new Reserve(slot.getCourt());
	}

	
		
}
